import { _decorator, Collider2D, Component, Contact2DType, IPhysics2DContact, RigidBody2D } from 'cc'
import { Actor } from '../Actor'
import { collisionTag } from '../CollisionTag'
const { ccclass, property } = _decorator
// 子弹逻辑
@ccclass('Projectile')
export class Projectile extends Component {
    rigidBody: RigidBody2D = null
    collider: Collider2D = null
    host: Actor | null = null;
    @property({ type: collisionTag.Define })
    hitTag: collisionTag.Define = collisionTag.Define.PlayerProjectile;
    onLoad() {

    }
    start() {
        this.rigidBody = this.node.getComponent(RigidBody2D)
        this.collider = this.node.getComponent(Collider2D)
        this.collider.tag = this.hitTag
        this.collider.on(Contact2DType.BEGIN_CONTACT, this.onContact, this)
    }
    onContact(self: Collider2D, other: Collider2D, contact: IPhysics2DContact) {
        if (collisionTag.isProjectileHitable(self.tag, other.tag)) {
            self.enabled = false
            // 节点销毁需要延迟一帧进行
            this.scheduleOnce(() => {
                this.node.destroy()
            }, 0.1)
        }
    }
    update(deltaTime: number) {

    }
    protected onDestroy(): void {
        this.collider.off(Contact2DType.BEGIN_CONTACT, this.onContact, this)
    }
}

